package cn.gzjp.modules.dt.service;

import java.util.*;

import cn.gzjp.modules.dt.entity.Question;
import cn.gzjp.modules.dt.entity.TestRecord;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gzjp.common.persistence.Page;
import cn.gzjp.common.service.CrudService;
import cn.gzjp.modules.dt.entity.Exam;
import cn.gzjp.modules.dt.dao.ExamDao;

/**
 * 考试Service
 * @author zengcp
 * @version 2018-02-24
 */
@Service
@Transactional(readOnly = true)
public class ExamService extends CrudService<ExamDao, Exam> {

	@Autowired
	private QuestionService questionService;

	public Exam get(String id) {
		return super.get(id);
	}
	
	public List<Exam> findList(Exam exam) {
		return super.findList(exam);
	}
	
	public Page<Exam> findPage(Page<Exam> page, Exam exam) {
		return super.findPage(page, exam);
	}
	
	@Transactional(readOnly = false)
	public void save(Exam exam) {
		super.save(exam);
	}
	
	@Transactional(readOnly = false)
	public void delete(Exam exam) {
		super.deleteByLogic(exam);
	}

	public void deleteByOpenid(String openid){
		dao.deleteByOpenid(openid);
	}

	/**
	 * 创建试卷
	 * @param openid
	 */
	@Transactional(readOnly = false)
	public List<Exam> createExamPaper(String openid){
		this.deleteByOpenid(openid);
		Question search = new Question();
//		search.setType("1");//单选题
//		List<Question> radioQuestions = questionService.findList(search);
//		search.setType("2");//多选题
//		List<Question> multiQuestions = questionService.findList(search);
//		search.setType("3");//判断题
//		List<Question> jugeQuestions = questionService.findList(search);
//		Set<Question> radioSet = random(radioQuestions,70);
//		Set<Question> multiSet = random(multiQuestions,20);
//		Set<Question> jugeSet = random(jugeQuestions,10);
//		List<Question> radioArray = new ArrayList<Question>(radioSet);
//		List<Question> multiArray = new ArrayList<Question>(multiSet);
//		List<Question> jugeArray = new ArrayList<Question>(jugeSet);
//		List<Question> result = Lists.newArrayList();
//		result.addAll(radioArray);
//		result.addAll(multiArray);
//		result.addAll(jugeArray);
		List<Question> allList = questionService.findList(search);
		int maxSn = allList.size()>100?100:allList.size();
		List<Question> result = new ArrayList<Question>(random(allList,maxSn));
		int sn=1;
		Date createTime = new Date();
		List<Exam> exams = Lists.newArrayList();
		for(Question question:result){
			Exam exam = new Exam();
			exam.setQuestion(question);
			exam.setOpenid(openid);
			exam.setSn(sn);
			exam.setCreateTime(createTime);
			this.save(exam);
			sn++;
			exams.add(exam);
		}
	return exams;

	}

	private static Set<Question> random(List<Question> questions, int n){
		if (n > questions.size()) {
			return null;
		}
		Set<Question> result = new HashSet<Question>();
		while(result.size() < n) {
			int num = (int) (Math.random() * (questions.size()));
			if(!result.contains(questions.get(num))){
				result.add(questions.get(num));
			}
		}
		return result;
	}

	Integer getCount(Exam exam){
		return dao.getCount(exam);
	}
	public Exam findByOpenidAndSn(String openid,Integer sn){
		Exam exam = new Exam();
		exam.setOpenid(openid);
		exam.setSn(sn);
		return dao.findByOpenidAndSn(exam);
	}

	public Integer getCorrectCount(String openid) {
		Exam record = new Exam();
		record.setOpenid(openid);
		return dao.getCorrectCount(record);
	}
	public Integer getErrorCount(String openid) {
		Exam record = new Exam();
		record.setOpenid(openid);
		return dao.getErrorCount(record);
	}

	public Integer getTotalCount(String openid) {
		Exam record = new Exam();
		record.setOpenid(openid);
		return dao.getTotalCount(record);
	}
}